<?php
include '../../../wp-blog-header.php';

$user_id = $_POST['user_id'];
$user_slug = $_POST['user_slug'];
if(empty($user_id) && empty($user_slug)){
  echo 'Invalid Session';
}
else{
  $userapi = new Pod('api');
  $userapi->page = 1;
  $userapi->search = 1;
  $userapi->findRecords('id DESC', -1, "api_user.id ='$user_id'");
  $userapi_unique = $userapi->getTotalRows();
  
  if($userapi_unique > 0)
  {
    while($userapi->fetchRecord())
    {
      $unique_api_key = $userapi->get_field('api_key');
    }
  }
  
  $width = $_POST['width'];
  $widget_color = $_POST['widget_color'];
  $type = $_POST['type'];
  $city = $_POST['city'];
  
  $widget_preset = $type.'~'.$city;
  
  
  if($userapi_unique == 0){
    $users_name = pods_sanitize($_POST['users_name']);
    $email = pods_sanitize($_POST['email']);
    $website = pods_sanitize($_POST['website']);

    function check_api_key($api_key){
      $api_pod = new Pod('api');
      $api_pod->page = 1;
      $api_pod->search = 1;
      $api_pod->findRecords('id DESC', -1, "t.api_key ='$api_key'");
      $is_api_unique = $api_pod->getTotalRows();
      if($is_api_unique > 0)
      {
        check_api_key(uniqid());  
      }
      else
      {
        return $api_key;
      }
    }
  
    $unique_api_key = check_api_key(uniqid());
    
    $api = new PodAPI();
    $columns_api['name'] = $users_name.'_'.$unique_api_key;
    $columns_api['slug'] = sanitize_title($columns_api['name']);
    $columns_api['api_user'] = $user_id;
    $columns_api['api_key'] = $unique_api_key;
    $columns_api['user_email'] = $email;
    $columns_api['user_website'] = $website;
  
    $params_api = array('datatype' => 'api','columns' => $columns_api);
    $api->save_pod_item($params_api);
     
    $sub = "Buzzr.in: Api Signup";
        
    $msg = '<div id="body" style="width: 500px; border: solid; border-color: #FFFFFF; background-color: #FFFFFF;">
    <div id="logo" style="text-align: left">
    	<img style="margin-left: 178px; margin-top: 10px;" height="40" src="http://buzzr.in/wordpress/wp-content/uploads/2011/01/buzzrin-logo.png" />
    </div>
    <div id="content" style="width: auto; margin: 20px; padding: 5px; background-color: #D0F0FB; font-size: 14px; color:#1BA3DF;">
    Hello,<br /><br />
    '.$users_name.' has been signed up for an API.<br /><br /> 
    <b>API details:</b><br />
    <b>Username: </b>'.$users_name.'<br />
    <b>Website: </b>'.$website.'<br />
    <b>Unique Key: </b>'.$unique_api_key.'<br /><br />
    Thank You!
    
    <font><i><br /><br />This email was sent to through Buzzr.in<br />
    If this message does not apply to you please ignore the email Thank You! 
    </i></font>
    </div>
    
    </div>';
    
    $headers = array("From: Buzzr.in <info@enthrallmedia.com>","Content-Type: text/html");
    $h = implode("\r\n",$headers) . "\r\n";
    wp_mail('chetan.bhatia@enthrallmedia.com', $sub, $msg, $h);
    wp_mail('kunal.jain@enthrallmedia.com', $sub, $msg, $h);  
  }
  
  $string = $unique_api_key.'$'.$widget_preset.'$'.$width.'px$'.$widget_color.'$'.$user_slug;
  $string = encrypt($string,'1b2u3z4z5r6');
  
  $user_code_url = BUZZR_WP_URL.'/buzzr_widget.php?p='.$string;
  
  $code = '<iframe src ="'.$user_code_url.'" width="'.($width + 20).'" height="350" scrolling="no" frameborder="0" allowtransparency="true" style="border: medium none; overflow: hidden;"></iframe><div><div style="font-size:10px;margin-left:10px;"><a href="http://buzzr.in/?utm_source=widget&utm_medium=cpc&utm_campaign=Buzzr+Widget" target="_blank">Deals Provided by Buzzr</a></div>';
  
  function printCode($code, $lines_number = 0)    {
              
         if (!is_array($code)) $codeE = explode("\n", $code);
        $count_lines = count($codeE);
       
        $r1 = "Code:<br />";

         if ($lines_number){           
                $r1 .= "<div class=\"linenum\">";
                foreach($codeE as $line =>$c) {    
                    if($count_lines=='1')
                        $r1 .= "1<br>";
                    else
                        $r1 .= ($line == ($count_lines - 1)) ? "" :  ($line+1)."<br />";
                 }
                 $r1 .= "</div>";
         }

         $r2 = "<div id=\"linetext\" class=\"linetext\">";
         $r2 .= highlight_string($code,1);
         $r2 .= "</div>";

        //$r .= $r1.$r2;
        $r .= $r2;
        echo "<b>Widget Code:</b> (Paste it on your website)<br /><div class=\"code\"><pre>".$r."</pre></div>\n";
    }

    printCode($code,1);
}
?>